/**
 * 嫌疑人实体类
 * 表示案件中的嫌疑人信息
 */
export enum SuspectStatus {
  UNKNOWN = 'unknown',      // 未知
  PERSON_OF_INTEREST = 'person_of_interest', // 关注对象
  WITNESS = 'witness',      // 证人
  SUSPECT = 'suspect',      // 嫌疑人
  PRIME_SUSPECT = 'prime_suspect', // 主要嫌疑人
  CLEARED = 'cleared',      // 已排除嫌疑
  ARRESTED = 'arrested',    // 已逮捕
  CONVICTED = 'convicted'   // 已定罪
}

export enum SuspectRole {
  VICTIM = 'victim',        // 受害者
  PERPETRATOR = 'perpetrator', // 犯罪者
  ACCOMPLICE = 'accomplice', // 共犯
  WITNESS = 'witness',      // 证人
  BYSTANDER = 'bystander',  // 旁观者
  AUTHORITY = 'authority'   // 权威人士
}

export enum SuspectMotive {
  FINANCIAL = 'financial',    // 金钱动机
  REVENGE = 'revenge',      // 报复动机
  JEALOUSY = 'jealousy',     // 嫉妒动机
  LOVE = 'love',           // 爱情动机
  POWER = 'power',         // 权力动机
  FEAR = 'fear',           // 恐惧动机
  INSANITY = 'insanity',    // 精神异常
  ACCIDENTAL = 'accidental', // 意外
  UNKNOWN = 'unknown'       // 未知动机
}

export interface Suspect {
  id: string;
  caseId: string;
  name: string;
  age: number;
  gender: 'male' | 'female' | 'other';
  occupation: string;
  address: string;
  phone: string;
  email: string;
  status: SuspectStatus;
  role: SuspectRole;
  motive: SuspectMotive;
  alibi: string;
  alibiVerified: boolean;
  criminalRecord: boolean;
  description: string;
  physicalDescription: string;
  psychologicalProfile: string;
  relationshipToVictim: string;
  opportunity: number; // 机会评分 0-100
  motiveStrength: number; // 动机强度 0-100
  suspiciousBehavior: string[];
  evidenceFor: string[]; // 支持证据ID
  evidenceAgainst: string[]; // 反对证据ID
  interviewNotes: string[];
  imageUrl?: string;
  lastSeenLocation: string;
  lastSeenTime: string;
  isKeySuspect: boolean;
  confidenceLevel: number; // 置信度 0-100
  tags: string[];
}

/**
 * 创建新嫌疑人的工厂函数
 */
export function createSuspect(data: Partial<Suspect> = {}): Suspect {
  return {
    id: data.id || generateSuspectId(),
    caseId: data.caseId || '',
    name: data.name || '未知姓名',
    age: data.age || 0,
    gender: data.gender || 'other',
    occupation: data.occupation || '未知职业',
    address: data.address || '未知地址',
    phone: data.phone || '',
    email: data.email || '',
    status: data.status || SuspectStatus.UNKNOWN,
    role: data.role || SuspectRole.BYSTANDER,
    motive: data.motive || SuspectMotive.UNKNOWN,
    alibi: data.alibi || '',
    alibiVerified: data.alibiVerified || false,
    criminalRecord: data.criminalRecord || false,
    description: data.description || '',
    physicalDescription: data.physicalDescription || '',
    psychologicalProfile: data.psychologicalProfile || '',
    relationshipToVictim: data.relationshipToVictim || '未知',
    opportunity: data.opportunity || 0,
    motiveStrength: data.motiveStrength || 0,
    suspiciousBehavior: data.suspiciousBehavior || [],
    evidenceFor: data.evidenceFor || [],
    evidenceAgainst: data.evidenceAgainst || [],
    interviewNotes: data.interviewNotes || [],
    imageUrl: data.imageUrl,
    lastSeenLocation: data.lastSeenLocation || '未知地点',
    lastSeenTime: data.lastSeenTime || '',
    isKeySuspect: data.isKeySuspect || false,
    confidenceLevel: data.confidenceLevel || 0,
    tags: data.tags || [],
    ...data
  };
}

/**
 * 生成嫌疑人ID
 */
function generateSuspectId(): string {
  const timestamp = Date.now();
  const random = Math.random().toString(36).substr(2, 9);
  return `SUSPECT-${timestamp}-${random}`;
}

/**
 * 获取嫌疑人状态的中文名称
 */
export function getSuspectStatusName(status: SuspectStatus): string {
  const statusNames = {
    [SuspectStatus.UNKNOWN]: '未知',
    [SuspectStatus.PERSON_OF_INTEREST]: '关注对象',
    [SuspectStatus.WITNESS]: '证人',
    [SuspectStatus.SUSPECT]: '嫌疑人',
    [SuspectStatus.PRIME_SUSPECT]: '主要嫌疑人',
    [SuspectStatus.CLEARED]: '已排除嫌疑',
    [SuspectStatus.ARRESTED]: '已逮捕',
    [SuspectStatus.CONVICTED]: '已定罪'
  };
  return statusNames[status];
}

/**
 * 获取嫌疑人角色的中文名称
 */
export function getSuspectRoleName(role: SuspectRole): string {
  const roleNames = {
    [SuspectRole.VICTIM]: '受害者',
    [SuspectRole.PERPETRATOR]: '犯罪者',
    [SuspectRole.ACCOMPLICE]: '共犯',
    [SuspectRole.WITNESS]: '证人',
    [SuspectRole.BYSTANDER]: '旁观者',
    [SuspectRole.AUTHORITY]: '权威人士'
  };
  return roleNames[role];
}

/**
 * 获取嫌疑人动机的的中文名称
 */
export function getSuspectMotiveName(motive: SuspectMotive): string {
  const motiveNames = {
    [SuspectMotive.FINANCIAL]: '金钱动机',
    [SuspectMotive.REVENGE]: '报复动机',
    [SuspectMotive.JEALOUSY]: '嫉妒动机',
    [SuspectMotive.LOVE]: '爱情动机',
    [SuspectMotive.POWER]: '权力动机',
    [SuspectMotive.FEAR]: '恐惧动机',
    [SuspectMotive.INSANITY]: '精神异常',
    [SuspectMotive.ACCIDENTAL]: '意外',
    [SuspectMotive.UNKNOWN]: '未知动机'
  };
  return motiveNames[motive];
}

/**
 * 计算嫌疑人可疑度评分
 */
export function getSuspectSuspicionScore(suspect: Suspect): number {
  let score = 0;
  
  // 状态评分
  const statusScores = {
    [SuspectStatus.UNKNOWN]: 0,
    [SuspectStatus.PERSON_OF_INTEREST]: 20,
    [SuspectStatus.WITNESS]: 10,
    [SuspectStatus.SUSPECT]: 60,
    [SuspectStatus.PRIME_SUSPECT]: 90,
    [SuspectStatus.CLEARED]: 0,
    [SuspectStatus.ARRESTED]: 95,
    [SuspectStatus.CONVICTED]: 100
  };
  
  score += statusScores[suspect.status] || 0;
  
  // 机会评分
  score += suspect.opportunity * 0.2;
  
  // 动机强度评分
  score += suspect.motiveStrength * 0.3;
  
  // 证据数量评分
  score += Math.min(suspect.evidenceAgainst.length * 10, 30);
  
  // 可疑行为评分
  score += Math.min(suspect.suspiciousBehavior.length * 5, 20);
  
  // 无不在场证明
  if (!suspect.alibiVerified && suspect.alibi) {
    score += 15;
  }
  
  // 有犯罪记录
  if (suspect.criminalRecord) {
    score += 10;
  }
  
  return Math.min(Math.round(score), 100);
}

/**
 * 获取嫌疑人状态颜色
 */
export function getSuspectStatusColor(status: SuspectStatus): string {
  const statusColors = {
    [SuspectStatus.UNKNOWN]: '#95a5a6',
    [SuspectStatus.PERSON_OF_INTEREST]: '#f39c12',
    [SuspectStatus.WITNESS]: '#3498db',
    [SuspectStatus.SUSPECT]: '#e67e22',
    [SuspectStatus.PRIME_SUSPECT]: '#e74c3c',
    [SuspectStatus.CLEARED]: '#27ae60',
    [SuspectStatus.ARRESTED]: '#c0392b',
    [SuspectStatus.CONVICTED]: '#8b0000'
  };
  return statusColors[status];
}

/**
 * 获取嫌疑人角色颜色
 */
export function getSuspectRoleColor(role: SuspectRole): string {
  const roleColors = {
    [SuspectRole.VICTIM]: '#3498db',
    [SuspectRole.PERPETRATOR]: '#e74c3c',
    [SuspectRole.ACCOMPLICE]: '#e67e22',
    [SuspectRole.WITNESS]: '#2ecc71',
    [SuspectRole.BYSTANDER]: '#95a5a6',
    [SuspectRole.AUTHORITY]: '#9b59b6'
  };
  return roleColors[role];
}